import React from 'react'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Modal 	= $Lazy.state(()=>import('@antd/modal'))
const Upload 	= $Lazy.hook(()=>import('@antd/upload'))
const message 	= $Lazy.file(()=>import('@antd/message'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		list:[],
	}
	
	componentDidMount(){
		
	}
	
	open = () => this.modal.open()
	
	render(){
		const { list, submitLoading } = this.state
		return (
			<Modal
				title		= '文件上传'
				width 		= '50%'
				height		= '60%'
				onRef		= { ref => this.modal = ref}
				isFullScroll
				okDisabled	= { !$fn.hasArray(list) }
				okText		= '开始上传'
				loading 	= { submitLoading }
				onOk		= { async ()=>{
					this.setState({ submitLoading:true })
					const data =  await this.uploadRef.upload()
					if($fn.hasArray(data)){
						await this.props?.onRefresh({ 
							data, 
							close: this.modal.close, 
							onError:()=>{ 
								this.setState({ submitLoading:false })
								message.then(f=>f.error('文件上传失败成功'))
							} 
						})
					}
					message.then(f=>f.success('上传成功'))
					this.setState({ submitLoading:false })
				}}
				close 		= { () => this.uploadRef.clear() }
			>
				{/* 修改上传文件、报告单、图片的接口地址和参数及返回结果处理 2021-10-9 --马婷 */}
				<Upload loading={submitLoading} onRef={ref => this.uploadRef = ref} fileType={['img','word','pdf','excel']} mode='button' hand btnText='选择文件'
					onChanged={({ value }) => this.setState({ list:value })}
				/>
			</Modal>
		)
	}
}

export default Index